Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(storage): Splitting table change log from HummockVersion on CN side #20050

Merged
merged 17 commits into from
Jan 20, 2025

Conversation

Li0k
Copy link
Contributor

@Li0k Li0k commented Jan 7, 2025

I hereby agree to the terms of the RisingWave Labs, Inc. Contributor License Agreement.

What's changed and what's your intention?

This PR optimize clone behavior on the CN side. Previously, the hummock event handler copied the hummock version every time it applied a delta, and the overhead of cloning could cause performance problems.issues.

let mut version_to_apply = pinned_version.version().clone();

This PR split the table change log from the hummock version to avoid copying all table change logs at each version delta.

Key changes include:

Enhancements to HummockVersion and HummockVersionDelta:

  • Introduced a second type parameter L to HummockVersionCommon and HummockVersionDeltaCommon to improve type safety and flexibility. Split table_change_log into separate fields and protect them with RwLock. (src/storage/hummock_sdk/src/version.rs, src/storage/hummock_sdk/src/compaction_group/hummock_version_ext.rs)

New Methods and Type Changes:

  • Added change_log_into_iter method to TableChangeLogCommon to allow iteration over change logs. (src/storage/hummock_sdk/src/change_log.rs)

Type Aliases:

  • Updated type aliases to use the new two-parameter versions of HummockVersionCommon and HummockVersionDeltaCommon. (src/storage/hummock_sdk/src/time_travel.rs, src/storage/hummock_sdk/src/version.rs)

Import Adjustments:

  • Adjusted imports to accommodate the new types and methods. (src/storage/hummock_sdk/src/compaction_group/hummock_version_ext.rs, src/storage/hummock_sdk/src/version.rs, src/storage/src/hummock/event_handler/hummock_event_handler.rs)

Checklist

  • I have written necessary rustdoc comments.
  • I have added necessary unit tests and integration tests.
  • I have added test labels as necessary.
  • I have added fuzzing tests or opened an issue to track them.
  • My PR contains breaking changes.
  • My PR changes performance-critical code, so I will run (micro) benchmarks and present the results.
  • My PR contains critical fixes that are necessary to be merged into the latest release.

Documentation

  • My PR needs documentation updates.
Release note

@Li0k Li0k changed the title feat(storage): Splitting table change log from HummockVersion on CN side WIP: feat(storage): Splitting table change log from HummockVersion on CN side Jan 7, 2025
@Li0k Li0k changed the title WIP: feat(storage): Splitting table change log from HummockVersion on CN side feat(storage): Splitting table change log from HummockVersion on CN side Jan 7, 2025
@Li0k Li0k requested review from wenym1, hzxa21 and zwang28 and removed request for wenym1 and hzxa21 January 7, 2025 08:45
@Li0k Li0k requested a review from hzxa21 January 7, 2025 08:45
guard: Arc::new(PinnedVersionGuard::new(
version_id,
self.guard.pinned_version_manager_tx.clone(),
)),
table_change_log: Arc::new(RwLock::new(t)),
version: Arc::new(LocalHummockVersion::from(version)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just want to leave a note here.

This LocalHummockVersion::from is an addtional HummockVersion conversion introduced by this PR. However I don't think it will have significant performance implications, as it primarily involves move semantics.

let change_log = {
let table_change_logs = version.table_change_log().read();
if let Some(change_log) = table_change_logs.get(&options.table_id) {
change_log.filter_epoch(epoch_range).cloned().collect_vec()
Copy link
Contributor

@zwang28 zwang28 Jan 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cloned() is an additional cost introduced in this PR.

If multiple iter_log are running simultaneously, will the memory usage be substantial?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @wenym1 , suggests that iter_log is executed less frequently and that this clone is acceptable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In current usage of iter_log, we only do iter_log on a single epoch, so this vector should be small.

Copy link
Contributor

@zwang28 zwang28 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Li0k
Copy link
Contributor Author

Li0k commented Jan 13, 2025

interfaces are affected:

  • iter_log
    - next_epoch

cc @wenym1

Copy link
Contributor

@wenym1 wenym1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rest LGTM. Thanks for the PR.

src/storage/hummock_sdk/src/version.rs Outdated Show resolved Hide resolved
src/storage/hummock_sdk/src/version.rs Outdated Show resolved Hide resolved
let change_log = {
let table_change_logs = version.table_change_log().read();
if let Some(change_log) = table_change_logs.get(&options.table_id) {
change_log.filter_epoch(epoch_range).cloned().collect_vec()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In current usage of iter_log, we only do iter_log on a single epoch, so this vector should be small.

src/storage/hummock_sdk/src/change_log.rs Outdated Show resolved Hide resolved
src/storage/hummock_sdk/src/time_travel.rs Outdated Show resolved Hide resolved
src/storage/src/hummock/local_version/pinned_version.rs Outdated Show resolved Hide resolved
src/storage/src/hummock/local_version/pinned_version.rs Outdated Show resolved Hide resolved
src/storage/src/hummock/local_version/pinned_version.rs Outdated Show resolved Hide resolved
.build_sst_delta_infos(version_delta)
.into_iter(),
let mut version_to_apply = pinned_version.version().clone();
let table_change_log_to_apply = pinned_version.take_change_log();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of take_change_log, we can have method change_log_write_lock to return the write lock on the table_change_log. And then in new_pin_version_with_table_change_log, we don't have to pass the table_change_log_to_apply again. We can use the original table_change_log wrapped by Arc and simply clone the Arc.

Besides, it seems that the parameter pinned_version of the current method is not necessary to take the ownership. We can change to pass &PinnedVersion, so that the caller can avoid doing clone on it.

@Li0k Li0k added this pull request to the merge queue Jan 17, 2025
@Li0k Li0k removed this pull request from the merge queue due to a manual request Jan 17, 2025
@Li0k Li0k enabled auto-merge January 17, 2025 07:51
…nto li0k/storage_divide_table_change_log
@Li0k Li0k added this pull request to the merge queue Jan 17, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jan 17, 2025
@@ -476,10 +473,26 @@ impl HummockVersion {
state_table_info_delta: Default::default(),
}
}

pub fn split_change_log(mut self) -> (LocalHummockVersion, HashMap<TableId, TableChangeLog>) {
let table_change_log = std::mem::take(&mut self.table_change_log);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here after std::mem::take(&mut self.table_change_log), the epochs in self.table_change_log becomes empty. But we still need to use the epochs in LocalHummockVersion

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch !!!

src/storage/hummock_sdk/src/version.rs Outdated Show resolved Hide resolved
src/storage/hummock_sdk/src/version.rs Outdated Show resolved Hide resolved
src/storage/src/hummock/local_version/pinned_version.rs Outdated Show resolved Hide resolved
Copy link

gru-agent bot commented Jan 20, 2025

This pull request has been modified. If you want me to regenerate unit test for any of the files related, please find the file in "Files Changed" tab and add a comment @gru-agent. (The github "Comment on this file" feature is in the upper right corner of each file in "Files Changed" tab.)

@Li0k
Copy link
Contributor Author

Li0k commented Jan 20, 2025

@wenym1 PTAL

Copy link
Contributor

@wenym1 wenym1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rest LGTM!

Comment on lines 481 to 492
let mut change_log = VecDeque::new();
for item in log.change_log_iter_mut() {
let new_value = EpochNewChangeLogCommon {
new_value: std::mem::take(&mut item.new_value),
old_value: std::mem::take(&mut item.old_value),
epochs: item.epochs.clone(),
};

change_log.push_back(new_value);
}
table_change_log
.insert(*table_id, TableChangeLogCommon::new(change_log.into_iter()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let mut change_log = VecDeque::new();
for item in log.change_log_iter_mut() {
let new_value = EpochNewChangeLogCommon {
new_value: std::mem::take(&mut item.new_value),
old_value: std::mem::take(&mut item.old_value),
epochs: item.epochs.clone(),
};
change_log.push_back(new_value);
}
table_change_log
.insert(*table_id, TableChangeLogCommon::new(change_log.into_iter()));
let change_log_iter = log.change_log_iter_mut().map(|item| {
EpochNewChangeLogCommon {
new_value: std::mem::take(&mut item.new_value),
old_value: std::mem::take(&mut item.old_value),
epochs: item.epochs.clone(),
}
}
table_change_log
.insert(*table_id, TableChangeLogCommon::new(change_log_iter));

@Li0k Li0k enabled auto-merge January 20, 2025 07:59
@Li0k Li0k added this pull request to the merge queue Jan 20, 2025
Merged via the queue into main with commit bf8f076 Jan 20, 2025
29 of 30 checks passed
@Li0k Li0k deleted the li0k/storage_divide_table_change_log branch January 20, 2025 09:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants